package com.android.clinic.app.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.android.clinic.app.R;
import com.android.clinic.app.entity.BannerInfo;
import com.android.clinic.app.ui.main.view.BannerView;
import com.android.clinic.app.utils.DisplayUtil;
import com.android.clinic.app.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/1/26.
 */
public class HomeBannerAdapter extends BaseDelegateAdapter {

    private List<BannerInfo> bannerInfoList = new ArrayList();
    private ViewHolder holder;
    private Context context;

    public HomeBannerAdapter(Context context, int... resource) {
        super(context, R.layout.item_home_banner);
        this.context = context;
    }

    @Override
    protected Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex) {
        return new ViewHolder(convertView);
    }

    @Override
    protected void setupBindViewHolder(Object viewHolder, int position, int typeIndex) {
        if (bannerInfoList.size() > 0) {
            holder = (ViewHolder) viewHolder;
            holder.bannerView.setArrayList(bannerInfoList);
        }
    }

    @Override
    public int getItemCount() {
        if (null != bannerInfoList && bannerInfoList.size() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.bannerView.stopBanner();
            viewHolder.bannerView.releaseBanner();
            viewHolder.bannerView.removeBanner();
        }
    }

    public void releaseBanner() {
        if (holder != null) {
            holder.bannerView.releaseBanner();
        }
    }

    public void stopBanner() {
        if (holder != null) {
            holder.bannerView.stopBanner();
        }
    }

    public void startBanner() {
        if (holder != null) {
            holder.bannerView.startBanner();
        }
    }

    public void setBannerData(List<BannerInfo> bannerInfoList) {
        releaseBanner();
        this.bannerInfoList = bannerInfoList;
        notifyDataSetChanged();
    }


    private class ViewHolder extends BaseViewHolder {

        private BannerView bannerView;

        public ViewHolder(View itemView) {
            super(itemView);
            bannerView = (BannerView) itemView.findViewById(R.id.banner_view);
            bannerView.init(context, Utils.getScreenWidth(), DisplayUtil.dip2px(750), DisplayUtil.dip2px(336));
        }
    }
}
